package com.coocaa.circleprogress;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.ColorInt;

/**
 * @ Created on: 2020/5/26
 * @Author: LEGION XiaoLuo
 * @ Description:
 */
public class CircleProgress extends View {
    private Paint mBackPaint;
    private Paint mProgressPaint;
    private float mPaintWidth = 4f;
    private float mWidth;
    private float mHeight;
    private float mStartAngle = -90;
    private float mSweepAngle = 90;

    public CircleProgress(Context context) {
        super(context);
        init();
    }

    public CircleProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        mBackPaint = new Paint();
        mBackPaint.setColor(Color.parseColor("#1A000000"));
        mBackPaint.setStyle(Paint.Style.STROKE);
        mBackPaint.setStrokeWidth(mPaintWidth);
        mBackPaint.setAntiAlias(true);

        mProgressPaint = new Paint();
        mProgressPaint.setColor(Color.BLACK);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(mPaintWidth);
        mProgressPaint.setAntiAlias(true);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    /**
     * @param color 需设置透明度
     */
    public void setBackPaintColor(@ColorInt int color) {
        mBackPaint.setColor(color);
    }

    /**
     * @param color 需设置透明度
     */
    public void setProgressPaintColor(@ColorInt int color) {
        mProgressPaint.setColor(color);
    }

    /**
     * @param arcWidth 设置圆弧的宽度
     */
    public void setArcWidth(float arcWidth) {
        mBackPaint.setStrokeWidth(arcWidth);
        mProgressPaint.setStrokeWidth(arcWidth);
    }

    /**
     * @param startAngle 进度条的开始位置，默认设置为-90
     */
    public void setStartAngle(float startAngle) {
        mStartAngle = startAngle;
    }

    /**
     * @param value    进度
     * @param maxValue 最大进度
     */
    public void refreshProgress(float value, float maxValue) {
        mSweepAngle = value / maxValue * 360;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mWidth / 2, mHeight / 2, (mWidth - mPaintWidth) / 2, mBackPaint);

        RectF rectF = new RectF(mPaintWidth / 2, mPaintWidth / 2, mWidth - mPaintWidth / 2, mHeight - mPaintWidth / 2);
        canvas.drawArc(rectF, mStartAngle, mSweepAngle, false, mProgressPaint);
    }
}
